home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.01 Jan 90 / Jan Jorg Source / MacTutorGrow.cp < prev    next >
Encoding:
Text File  |  1989-11-20  |  9.3 KB  |  369 lines  |  [TEXT/MPS ]

  1. #include <Types.h>
  2. #include <QuickDraw.h>
  3. #include <Fonts.h>
  4. #include <Events.h>
  5. #include <OSEvents.h>
  6. #include <Controls.h>
  7. #include <Windows.h>
  8. #include <Menus.h>
  9. #include <TextEdit.h>
  10. #include <Dialogs.h>
  11. #include <Desk.h>
  12. #include <Scrap.h>
  13. #include <ToolUtils.h>
  14. #include <Memory.h>
  15. #include <SegLoad.h>
  16. #include <Files.h>
  17. #include <OSUtils.h>
  18. #include <Traps.h>
  19. #include <StdLib.h>
  20.  
  21. #include "TDocument.h"
  22. #include "TApplication.h"
  23. #include "MacTutorApp.h"
  24. #include "MacTutorDoc.h"
  25. #include "MacTutorGrow.h"
  26.  
  27. // constants used for scroll bar and grow box size adjustment
  28. const short kScrollbarAdjust = 15;
  29. const short kGrowboxAdjust = 15;
  30. const short kScrollbarWidth = 16;
  31. const short kScrollTweek = 2;
  32. const short kControlVisible = 0xFF;
  33.  
  34. extern "C" { 
  35.     // prototypes for functions that don't belong to any one class
  36.     void CommonAction(ControlHandle control,short* amount);
  37.     pascal void VActionProc(ControlHandle control,short part);
  38.     pascal void HActionProc(ControlHandle control,short part);
  39. };
  40.  
  41. // methods for the MacTutorGrow class
  42.  
  43. // create and delete document windows
  44. // override methods from MacTutorDocument
  45. // The base class creates the window and sets the display string.
  46. // We try to get the controls, and display an error if we can't
  47. //
  48. TMacTutorGrow::TMacTutorGrow(short resID, StringPtr s)    : (resID,s)
  49. {
  50.     Boolean good;
  51.     fDocVScroll = GetNewControl(rVScroll, fDocWindow);
  52.     good = (fDocVScroll != nil);
  53.     if ( good)
  54.       {
  55.         fDocHScroll = GetNewControl(rHScroll, fDocWindow);
  56.         good = (fDocHScroll != nil);
  57.       }
  58.     if ( good )                // good? — adjust & draw the controls
  59.       {
  60.         AdjustScrollValues(true);
  61.       }
  62.     else
  63.       {
  64.         // tell user we failed
  65.         HideWindow(fDocWindow);
  66.         AlertUser(kErrStrings,eNoWindow); 
  67.       }
  68. }
  69.  
  70. TMacTutorGrow::~TMacTutorGrow(void)
  71. {
  72.     HideWindow(fDocWindow);
  73.     if ( fDocVScroll != nil )
  74.       { DisposeControl(fDocVScroll); }
  75.     if ( fDocHScroll != nil )
  76.       { DisposeControl(fDocHScroll); }
  77. }
  78.  
  79. void TMacTutorGrow::DoUpdate(void)
  80. {
  81.  
  82.     BeginUpdate(fDocWindow);            
  83.     if ( ! EmptyRgn(fDocWindow->visRgn) ) 
  84.       {
  85.         DrawWindow();
  86.       }
  87.     EndUpdate(fDocWindow);
  88. }
  89.  
  90. void TMacTutorGrow::DrawWindow(void)
  91. {
  92.     Rect tRect;
  93.     SetPort(fDocWindow);
  94.     tRect = fDocWindow->portRect;
  95.     tRect.bottom = tRect.bottom - kScrollbarAdjust;
  96.     tRect.right = tRect.right - kScrollbarAdjust;
  97.     EraseRect(&tRect);
  98.     
  99.     MoveTo(100,100);
  100.     TextSize(18); TextFont(monaco);
  101.     DrawString(fDisplayString);
  102.     DrawControls(fDocWindow);
  103.     DrawGrowIcon(fDocWindow);
  104.  
  105. } // DrawWindow
  106.  
  107. void TMacTutorGrow::DoActivate(Boolean becomingActive)
  108. {
  109.     if ( becomingActive )
  110.       {
  111.         Rect        growRect;
  112.         Rect        tRect;
  113.  
  114.         /* the controls must be redrawn on activation: */
  115.         (*fDocVScroll)->contrlVis = kControlVisible;
  116.         (*fDocHScroll)->contrlVis = kControlVisible;
  117.         // copy rectangles to avoid unsafe object field references!
  118.         tRect = (*fDocVScroll)->contrlRect; InvalRect(&tRect);
  119.         tRect = (*fDocHScroll)->contrlRect; InvalRect(&tRect);
  120.         // the growbox needs to be redrawn on activation:
  121.         growRect = fDocWindow->portRect;
  122.         // adjust for the scrollbars
  123.         growRect.top = growRect.bottom - kScrollbarAdjust;
  124.         growRect.left = growRect.right - kScrollbarAdjust;
  125.         InvalRect(&growRect);
  126.       }
  127.     else
  128.       {        
  129.         /* the controls must be hidden on deactivation: */
  130.         HideControl(fDocVScroll);
  131.         HideControl(fDocHScroll);
  132.         // we draw grow icon immediately, since we deactivate controls
  133.         // immediately, and the update delay looks funny
  134.         DrawGrowIcon(fDocWindow);
  135.       }
  136. }
  137.  
  138. void TMacTutorGrow::AdjustScrollSizes(void)
  139. {
  140.     MoveControl(fDocVScroll, 
  141.                     fDocWindow->portRect.right - kScrollbarAdjust, 
  142.                     fDocWindow->portRect.top);
  143.     SizeControl(fDocVScroll, kScrollbarWidth,
  144.                 fDocWindow->portRect.bottom - fDocWindow->portRect.top -
  145.                     kGrowboxAdjust + kScrollTweek);
  146.     MoveControl(fDocHScroll, 
  147.                 fDocWindow->portRect.left, 
  148.                 fDocWindow->portRect.bottom - kScrollbarAdjust);
  149.     SizeControl(fDocHScroll,
  150.                 fDocWindow->portRect.right - fDocWindow->portRect.left -
  151.                     kGrowboxAdjust + kScrollTweek,
  152.                 kScrollbarWidth);
  153. } // AdjustScrollSizes
  154.  
  155. void TMacTutorGrow::AdjustScrollbars(Boolean needsResize)
  156. {
  157.     (*fDocVScroll)->contrlVis = 0;
  158.     (*fDocHScroll)->contrlVis = 0;
  159.     if ( needsResize )  AdjustScrollSizes();
  160.     AdjustScrollValues(needsResize);
  161.     // restore visibility in case we never had to draw during adjustment 
  162.     (*fDocVScroll)->contrlVis = 0xff;
  163.     (*fDocHScroll)->contrlVis = 0xff;
  164. } // AdjustScrollbars 
  165.  
  166.  
  167. void TMacTutorGrow::AdjustScrollValues(Boolean mustRedraw)
  168. {
  169.     SetCtlMin(fDocVScroll,0);
  170.     SetCtlMax(fDocVScroll,1000);
  171.     SetCtlValue(fDocVScroll,500);
  172.     SetCtlMin(fDocHScroll,0);
  173.     SetCtlMax(fDocHScroll,1000);
  174.     SetCtlValue(fDocHScroll,500);
  175.     if ( mustRedraw )
  176.     {
  177.         ShowControl(fDocVScroll);
  178.         ShowControl(fDocHScroll);
  179.     }
  180. } // AdjustScrollValues
  181.  
  182. void TMacTutorGrow::DoZoom(short partCode)
  183. {
  184.     Rect tRect;
  185.  
  186.     tRect = fDocWindow->portRect;
  187.     EraseRect(&tRect);
  188.     ZoomWindow(fDocWindow, partCode, fDocWindow == FrontWindow());
  189.     AdjustScrollbars(true);        // adjust, redraw anyway 
  190.     InvalRect(&tRect);            // invalidate the whole content 
  191.     // the scrollbars were taken care of by AdjustScrollbars, so validate ’em 
  192.     tRect = (*fDocVScroll)->contrlRect;
  193.     ValidRect(&tRect);
  194.     tRect = (*fDocHScroll)->contrlRect;
  195.     ValidRect(&tRect);
  196. }
  197.  
  198. void TMacTutorGrow::DoGrow(EventRecord* theEvent)
  199. {
  200.     long growResult;
  201.     Rect tRect, tRect2;
  202.     
  203.     tRect = qd.screenBits.bounds;
  204.     tRect.left = kMinDocDim;
  205.     tRect.top = kMinDocDim;
  206.     tRect2 = fDocWindow->portRect;
  207.     tRect2.bottom = tRect2.bottom - kScrollbarAdjust;
  208.     tRect2.right = tRect2.right - kScrollbarAdjust;
  209.     growResult = GrowWindow(fDocWindow, theEvent->where, &tRect);
  210.     // see if it really changed size 
  211.     if ( growResult != 0 )
  212.       {
  213.         SizeWindow(fDocWindow, LoWrd(growResult), HiWrd(growResult), true);
  214.         AdjustScrollbars(true);
  215.         // calculate & validate the region that hasn’t changed so it won’t get redrawn
  216.         // Note: we copy rectangles so that we don't take address of object fields.
  217.         tRect = fDocWindow->portRect; 
  218.         (void) SectRect(&tRect, &tRect2, &tRect2);
  219.         InvalRect(&tRect); ValidRect(&tRect2);
  220.         tRect2 = (*fDocVScroll)->contrlRect; ValidRect(&tRect2);
  221.         tRect2 = (*fDocHScroll)->contrlRect; ValidRect(&tRect2);
  222.         // redraw grow icon
  223.         tRect.top = tRect.bottom - kScrollbarAdjust;
  224.         tRect.left = tRect.right - kScrollbarAdjust;
  225.         InvalRect(&tRect);
  226.       }
  227. }
  228.  
  229. void TMacTutorGrow::DoContent(EventRecord* theEvent)
  230. {
  231.     Point mouse;
  232.     ControlHandle control;
  233.     short part, value;
  234.     Rect tRect;
  235.     
  236.     SetPort(fDocWindow);
  237.     mouse = theEvent->where;
  238.     GlobalToLocal(&mouse);
  239.     
  240.     tRect = fDocWindow->portRect; 
  241.     tRect.bottom = tRect.bottom - kScrollbarAdjust;
  242.     tRect.right = tRect.right - kScrollbarAdjust;
  243.     part = FindControl(mouse, fDocWindow, &control);
  244.     switch ( part )
  245.     {
  246.         case 0: break;
  247.         case inThumb:
  248.             value = GetCtlValue(control);
  249.             part = TrackControl(control, mouse, nil);
  250.             if ( part != 0 )
  251.             {
  252.                 value -= GetCtlValue(control);
  253.                 // value now has CHANGE in value; if value changed, scroll 
  254.                 if ( value != 0 )
  255.                 {
  256.                     if ( control == fDocVScroll )
  257.                     {
  258.                         ScrollRect (&tRect, 0, value, nil);
  259.                         SetOrigin(tRect.left,tRect.top-value);
  260.                     }
  261.                     else 
  262.                     {
  263.                         ScrollRect (&tRect, value, 0, nil);
  264.                         SetOrigin(tRect.left-value,tRect.top);
  265.                     }
  266.                     AdjustScrollSizes();
  267.                 }
  268.             }
  269.             break;    
  270.         default:                        // they clicked in an arrow, so track & scroll 
  271.             if ( control == fDocVScroll )
  272.                 value = TrackControl(control, mouse, (ProcPtr) VActionProc);
  273.             else value = TrackControl(control, mouse, (ProcPtr) HActionProc);
  274.             AdjustScrollSizes();
  275.             break;
  276.     }
  277. }
  278.  
  279. // Common algorithm for pinning the value of a control. It returns the actual amount
  280. // the value of the control changed.
  281.  
  282. void CommonAction(ControlHandle control,short* amount)
  283. {
  284.     short        value, max;
  285.     
  286.     value = GetCtlValue(control);
  287.     max = GetCtlMax(control);
  288.     *amount = value - *amount;
  289.     if ( *amount <= 0 )
  290.         *amount = 0;
  291.     else if ( *amount >= max )
  292.         *amount = max;
  293.     SetCtlValue(control, *amount);
  294.     *amount = value - *amount;
  295. } // CommonAction 
  296.  
  297.  
  298. pascal void VActionProc(ControlHandle control,short part)
  299. {
  300.     short        amount;
  301.     WindowPtr    window;
  302.     Rect tRect;
  303.  
  304.     if ( part != 0 )
  305.       {
  306.         window = (*control)->contrlOwner;
  307.         tRect = window->portRect; 
  308.         tRect.bottom = tRect.bottom - kScrollbarAdjust;
  309.         tRect.right = tRect.right - kScrollbarAdjust;
  310.         switch ( part )
  311.           {
  312.             case inUpButton:
  313.             case inDownButton:        // 5 pixels
  314.                 amount = 5;
  315.                 break;
  316.             case inPageUp:            // 50 pixels
  317.             case inPageDown:
  318.                 amount = 50;
  319.                 break;
  320.           }
  321.         if ( (part == inDownButton) || (part == inPageDown) )
  322.             amount = -amount;        // reverse direction 
  323.         CommonAction(control, &amount);
  324.         if ( amount != 0 )
  325.         {
  326.             ScrollRect (&tRect, 0, amount, nil);
  327.             SetOrigin(tRect.left,tRect.top-amount);
  328.         }
  329.       }
  330. } // VActionProc 
  331.  
  332. // Determines how much to change the value of the horizontal scrollbar by and how
  333. // much to scroll the TE record.
  334.  
  335. pascal void HActionProc(ControlHandle control,short part)
  336. {
  337.     short        amount;
  338.     WindowPtr    window;
  339.     Rect tRect;
  340.  
  341.     if ( part != 0 )
  342.       {
  343.         window = (*control)->contrlOwner;
  344.         tRect = window->portRect; 
  345.         tRect.bottom = tRect.bottom - kScrollbarAdjust;
  346.         tRect.right = tRect.right - kScrollbarAdjust;
  347.         switch ( part )
  348.           {
  349.             case inUpButton:
  350.             case inDownButton:        // 5 pixels
  351.                 amount = 5;
  352.                 break;
  353.             case inPageUp:            // 50 pixels
  354.             case inPageDown:
  355.                 amount = 50;
  356.                 break;
  357.           }
  358.         if ( (part == inDownButton) || (part == inPageDown) )
  359.             amount = -amount;        // reverse direction 
  360.         CommonAction(control, &amount);
  361.         if ( amount != 0 )
  362.         {
  363.             ScrollRect (&tRect, amount, 0, nil);
  364.             SetOrigin(tRect.left-amount,tRect.top);
  365.         }
  366.       }
  367. } // HActionProc 
  368.  
  369.